﻿package disjunction.extensions.math
{
	public class MathUtils
	{
		public static function wrapValue(val:Number, min:Number, max:Number):Number
		{
			var zeroBasedMax:Number = max - min;
			if (zeroBasedMax < 0)
				throw new Error("Min must be less or equal to than max.");

			//the min becomes zero for calc purposes
			//this allows us to modulo val properly.
			var zeroBasedVal:Number = val - min;
			if (zeroBasedVal < 0)
				zeroBasedVal = zeroBasedMax + zeroBasedVal;
			if (zeroBasedVal > zeroBasedMax)
				zeroBasedVal %= zeroBasedMax;
				
			return zeroBasedVal + min; //get it back into the original range
		}
		
		//isn't this just lerp?
		public static function getSteppedValuesBetween
			(numStepsExclusive:uint, min:Number, max:Number/*, inclusive:Boolean = true*/):Vector.<Number>
		{			
			var steps:Vector.<Number> = new Vector.<Number>();
			var diff:Number = max - min;
			var stepDiff:Number = diff / (numStepsExclusive + 1);
			var accumulator:Number = min;
			for (var i:int = 0; i < numStepsExclusive + 2; i++)
			{
				/*
				if (!inclusive)
				{
					if (i != 0 && i != numStepsExclusive + 2)
						steps.push(min + i * stepDiff);
				}
				else
				*/
				steps.push(min + (i * stepDiff));
			}
			
			return steps;
		}
		
		public static function clamp(value:Number, min:Number, max:Number):Number
		{
			return Math.max(min, Math.min(max, value));
		}
		
		public static function isInRange(value:Number, min:Number, max:Number):Boolean
		{
			return value >= min && value <= max;
		}
	}
}